unit scriptsform;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, SynEdit, SynHighlighterJScript,
  SynHighlighterPas, BCLabel, BCButton, Forms, Controls, Graphics, Dialogs,
  ExtCtrls, StdCtrls, ComCtrls, nkTitleBar;

type

  { TfrmScripts }

  TfrmScripts = class(TForm)
    BCButton2: TBCButton;
    BCButton3: TBCButton;
    BCButton4: TBCButton;
    BCButton5: TBCButton;
    BCLabel1: TBCLabel;
    Label1: TLabel;
    ls: TListView;
    nkTitleBar1: TnkTitleBar;
    Panel1: TPanel;
    Panel2: TPanel;
    procedure BCButton3Click(Sender: TObject);
    procedure BCLabel1Click(Sender: TObject);
    procedure FormShow(Sender: TObject);
  private
  public
    procedure CopyScripts;
    procedure RefreshList;//刷新列表
    procedure FillKind(AList:TStrings);//填充分类
  end;

var
  frmScripts: TfrmScripts;

implementation

uses
  datamodule, editscriptform;

var
  tmpScripts:array of TnkScript;

{$R *.frm}

{ TfrmScripts }

procedure TfrmScripts.BCLabel1Click(Sender: TObject);
begin
  ModalResult:=mrCancel;
end;

procedure TfrmScripts.FormShow(Sender: TObject);
begin
  CopyScripts;
  //填充列表
  RefreshList;
end;

procedure TfrmScripts.CopyScripts;
var
  i:integer;
begin
  //从DM复制脚本数组
  SetLength(tmpScripts, length(dm.Scripts));
  for i:=0 to Length(tmpScripts)-1 do
  begin
    tmpScripts[i].Mode:=Dm.Scripts[i].Mode;
    tmpScripts[i].Kind:=Dm.Scripts[i].Kind;
    tmpScripts[i].Caption:=Dm.Scripts[i].Caption;
    tmpScripts[i].Url:=Dm.Scripts[i].Url;
    tmpScripts[i].Script:=Dm.Scripts[i].Script;
  end;
end;

procedure TfrmScripts.RefreshList;
var
  i:integer;
begin
  ls.Items.BeginUpdate;
  ls.Items.Clear;
  for i:=0 to Length(tmpScripts)-1 do
  begin
    with ls.Items.Add do
    begin
      Caption:=tmpScripts[i].Mode;
      SubItems.Add(tmpScripts[i].Kind);
      SubItems.Add(tmpScripts[i].Caption);
      SubItems.Add(tmpScripts[i].Url);
      SubItems.Add(inttostr(i));
    end;
  end;
  ls.Items.EndUpdate;
end;

procedure TfrmScripts.FillKind(AList: TStrings);
var
  i:integer;
begin
  AList.Clear;
  for i:=0 to Length(tmpScripts)-1 do
  begin
    if AList.IndexOf(tmpScripts[i].Kind)<0 then
    begin
      AList.Add(tmpScripts[i].Kind);
    end;
  end;
end;

procedure TfrmScripts.BCButton3Click(Sender: TObject);
var
  m:integer;
begin
  //新增脚本
  frmEditScript:=TfrmEditScript.Create(Application);
  frmEditScript.Label1.Caption:='新增自动化例程';
  //填充分类
  FillKind(frmEditScript.ComboBox2.Items);
  frmEditScript.ComboBox1.ItemIndex:=0;
  if frmEditScript.ShowModal=mrOK then
  begin
    //新增到tmpscripts数组
    m:=Length(tmpScripts);
    SetLength(tmpScripts,m+1);
    m:=Length(tmpScripts)-1;
    tmpScripts[m].Mode:=frmEditScript.ComboBox1.Text;
    tmpScripts[m].Kind:=Trim(frmEditScript.ComboBox2.Text);
    tmpScripts[m].Caption:=Trim(frmEditScript.Edit1.Text);
    tmpScripts[m].Url:=Trim(frmEditScript.Edit2.Text);
    tmpScripts[m].Script:=Trim(frmEditScript.SynEdit1.Lines.Text);
    //刷新列表
    RefreshList;
  end;
  frmEditScript.Free;
  frmEditScript:=nil;
end;

end.

